#include "SoftGPU.h"
#include <string>
#include <DirectXMath.h>
#include <windows.h>
#include <QtGui/QtGui>
#include <QtWidgets/QWidget>
#include <QtWidgets/QApplication>

struct ImageWidget : QWidget{
	ImageWidget(QWidget* parent, int width, int height, uchar* imageData) :
		QWidget(parent), 
		image(imageData, width, height, QImage::Format_RGB888)
	{
		resize(width, height);
		image = image.mirrored();
	}

	void paintEvent(QPaintEvent *event)
	{
		QPainter painter(this);
		painter.drawImage(0, 0, image, 0,0, image.width(), image.height());
	}

	QImage image;
};


int main(int argc, char* argv[])
{
	std::string modelPath("bunny_200.obj");
	int width = 512;
	int height = 512;
	float fov = 45.f;//angle

	float ranFov = DirectX::XMConvertToRadians(fov);

	auto pos = DirectX::XMVectorSet(0.f, 1.f, 2.f ,1.f);
	auto look = DirectX::XMVectorSet(0.f, 0.f, -1.f, 0.f);
	look = DirectX::XMVector3Normalize(look);
	auto up = DirectX::XMVectorSet(0.f, 1.f, 0.f, 0.f);

	auto worldMatrix = DirectX::XMMatrixScaling(4.f, 4.f, 4.f);

	auto viewMatrix = DirectX::XMMatrixLookToLH(pos, look, up);
	auto projMatrix = DirectX::XMMatrixPerspectiveFovLH(ranFov, static_cast<float>(width)/static_cast<float>(height), 0.1f, 10.f);
	auto finalMatrix = worldMatrix * viewMatrix * projMatrix;

	mz::SoftGPU gpu;
	gpu.Init(modelPath, finalMatrix, width, height);
 	auto imageData = gpu.Scan();

	QApplication app(argc, argv);
	ImageWidget widget(nullptr, width, height, imageData);
	QImage image(imageData, width, height, QImage::Format_RGB888);
	widget.show();
	return app.exec();
}

